package logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;


public class LoggerWriter {
	
	static LoggerWriter loggerWriter;
	private Logger logger; 
	private Level consoleLevel;
	
	public LoggerWriter() throws SecurityException, IOException
	{
		FileHandler handler = new FileHandler("log.txt");
		logger = Logger.getLogger("logFile");
		SimpleFormatterExtends simpleFrom  = new SimpleFormatterExtends();
		handler.setFormatter(simpleFrom);
		this.logger.addHandler(handler);
		this.logger.setUseParentHandlers(false);
		consoleLevel = Level.WARNING;
		
	}
	
	public void log(String msg,Level l)
	{
		logger.log(l, msg);
		if (consoleLevel.intValue() <= l.intValue())
		{
			String time = "HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(time);
			Calendar cal = Calendar.getInstance();
			cal.getTime();
			System.err.println(l +" " + msg + " " + sdf.format(cal.getTime()));
		}
	}
	
	public void setLogLevel(Level l)
	{
		logger.setLevel(l);
	}
	
	public void setConsoleLevel(Level l)
	{
		this.consoleLevel=l;
	}
	
	public static LoggerWriter getLoggerWriter() throws SecurityException, IOException
	{
		if (loggerWriter == null)
			loggerWriter = new LoggerWriter();
		return loggerWriter;
	}
	

}
